<?php

namespace Syrius\Query;

/**
 * Enter description here ...
 * @package	Query
 * @author	Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link	http://code.google.com/p/syrius/
 *
 */ abstract class Table {
	public function __toString() {
	}

}

/**
 * Enter description here ...
 * @package	Query
 * @author	Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link	http://code.google.com/p/syrius/
 *
 */
class Table_Create extends Table {
	/**
	 * nome della tabella
	 */
	private $_tblName;

	private $_createDefinition;

	private $_tableOptions;

	private $_partitionOptions;

	private $_selectStatement;

	/**
	 * LIKE old_tbl_name
	 */
	private $_oldTblName;

	public function addColumn($fieldName, $fieldDef) {
		$this->_fields [] = array($fieldName => $fieldDef);
	}

	private $_colModel;

	public function __construct($tableName) {
		$this->tbl_name = $tableName;
	}

	public function __toString() {
		$syntax = "CREATE TABLE `" . $this->tbl_name . "` (\n";
		$fields = array();
		foreach ($this->_fields as $k => $v) {
			$key = array_keys($v);
			$val = array_values($v);
			if ($val [0] != "")
				$fields [] = $key [0] . " " . $val [0];
		}
		$syntax .= implode(",\n", $fields);
		if (count($this->_keys) > 0)
			$syntax .= "," . implode(",\n", $this->_keys);

		$this->_engine = \Syrius\Config\Config::getEngine();
		$this->_charset = \Syrius\Config\Config::getCharset();
		$this->_collation = \Syrius\Config\Config::getCollation();
		$syntax .= "\n) ENGINE=" . $this->_engine . " DEFAULT CHARSET=" . $this->_charset . " COLLATE=" . $this->_collation . ";";
		return $syntax;
	}

	/**
	 * Funzione che aggiunge una Syrius_Keys
	 */
	public function addKey($key) {
		$this->_keys [] = $key;
	}

	private $_keys = array();

	private $_fields = array();

}

/**
 * Enter description here ...
 * @package	Query
 * @author	Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link	http://code.google.com/p/syrius/
 *
 */
class Table_Alter extends Table {
	private $_keys = array();

	private $_fields = array();

	/**
	 * nome della tabella
	 */
	private $_tblName;

	private $_createDefinition;

	private $_tableOptions;

	private $_partitionOptions;

	private $_selectStatement;

	/**
	 * LIKE old_tbl_name
	 */
	private $_oldTblName;

	private $_colModel;

	public function __construct($tableName) {
		$this->_addFields = array();
		$this->_updateFields = array();
		$this->_delFields = array();
		$this->tbl_name = $tableName;
	}

	/**
	 *
	 */
	public function __toString() {
		$preSyntax = "ALTER TABLE `" . $this->tbl_name . "` \n";
		$syntax = array();
		$fields = array();
		foreach ($this->_addFields as $k => $v) {
			$key = array_keys($v);
			$val = array_values($v);
			$val = $val [0];
			if ($val == "")
				continue;
			$field = $v [$key [0]];
			if ($field->getAttribute("extra_type") != "") {
				$val = str_replace("CONSTRAINT", "ADD CONSTRAINT", $val);
			}
			$fields [] = "ADD " . $key [0] . " " . $val;
		}
		if (count($fields))
			$syntax [] = implode(",\n", $fields);
		$fields = array();
		foreach ($this->_delFields as $item) {
			$fields [] = "DROP " . $item->Field;
			if ($item->Key == "MUL") {
				$fields [] = "DROP FOREIGN KEY frk_" . $this->tbl_name . "_" . $item->Field;
				$fields [] = "DROP INDEX frk_" . $this->tbl_name . "_" . $item->Field;
			}

		}
		if (count($fields))
			$syntax [] = implode(",\n", $fields);
		$fields = array();
		foreach ($this->_updateFields as $k => $v) {
			$key = array_keys($v);
			$val = array_values($v);
			// migliorare la procedura di alter di una colonna
			$fields [] = "CHANGE " . $key [0] . " " . $val [0];
		}
		foreach ($this->_keys as $k => $v) {
			$fields [] = "ADD INDEX (" . $v->getName() . ")";
		}
		if (count($fields)) {
			$syntax [] = implode(",\n", $fields);
		}
		$this->_engine = \Syrius\Config\Config::getEngine();
		$this->_charset = \Syrius\Config\Config::getCharset();
		$this->_collation = \Syrius\Config\Config::getCollation();
		if (count($syntax))
			return $preSyntax . implode(",\n", $syntax); else
			return "";
	}

	public function addColumn($fieldName, $fieldDef) {
		$this->_addFields [] = array($fieldName => $fieldDef);
	}

	/**
	 * Funzione che aggiunge una Syrius_Keys
	 */
	public function addKey($key) {
		$this->_keys [] = $key;
	}

	public function delColumn($field) {
		$this->_delFields [] = $field;
	}

	public function updateColumn($fieldOld, $fieldNew) {
		$fieldName = $fieldOld->Field;
		$fieldNew->removeKeys();
		$this->_updateFields [] = array($fieldName => $fieldName . " " . $fieldNew);
	}

}

?>
